package com.java2401.yangnan.util;

import java.util.HashMap;
import java.util.Map;

/**
 * description: 返回结果通用对象  Map
 */
public class R {

    /**
     * 通用成功状态码
     */
    public static final String SUCCESS_CODE = "001";
    /**
     * 失败状态码
     */
    public static final String FAIL_CODE = "004";
    /**
     * 未登录
     */
    public static final String USER_NO_LOGIN = "401";

    private String code;
    // 是一个常用的注解属性，它用于指示 Jackson 序列化库在序列化对象时，如果属性的值为 null，则不包含这个属性

    private String msg;

    private Object data;

    private Long  total;
    
    private Integer totalPage;
    
    /**
     * 新增的构造函数，目的是为了在前端分页条计算最后一页时需要的总页数
     * @param code
     * @param msg
     * @param data
     * @param total
     * @param totalPage
     */
    public R(String code, String msg, Object data, Long total, Integer totalPage) {
    	this.code = code;
    	this.msg = msg;
    	this.data = data;
    	this.total = total;
    	this.totalPage = totalPage;
    }
    
    
    public R(String code, String msg, Object data, Long total) {
    	this.code = code;
    	this.msg = msg;
    	this.data = data;
    	this.total = total;
    }

    /**
     * 成功
     * @param msg
     * @param data
     * @return
     */
    public static R ok(String msg,Object data,Long total, Integer totalPage){

        return new R(SUCCESS_CODE,msg,data,total,totalPage);
    }
    /**
     * 成功
     * @param data
     * @return
     */
    public static R ok(String msg,Object data){

        return ok(msg,data,null,null);
    }
    /**
     * 成功
     * @return
     */
    public static R ok(String msg){

        return ok(msg,null);
    }
    /**
     * 成功
     * @return
     */
    public static R ok(Object data){

        return ok(null,data);
    }
    /**
     * 失败
     * @param msg
     * @param data
     * @return
     */
    public static R fail(String msg,Object data,Long total){

        return new R(FAIL_CODE,msg,data,total);
    }
    
    public static R fail(String msg,Object data,Long total, Integer totalPage){

        return new R(FAIL_CODE,msg,data,total,totalPage);
    }
    /**
     * 失败
     * @param data
     * @return
     */
    public static R fail(String msg,Object data){

        return fail(msg,data,null);
    }
    /**
     * 失败
     * @return
     */
    public static R fail(String msg){

        return fail(msg,null);
    }
    /**
     * 失败
     * @return
     */
    public static R fail(Object data){

        return fail(null,data);
    }
    /**
     * 未登录
     * @return
     */
    public static R NO_LOGIN(){

        return fail(USER_NO_LOGIN,"用户未登录!");
    }
}